package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.JTextField;
import javax.swing.border.AbstractBorder;

@SuppressWarnings("serial")
public class RoundedTextField extends JTextField {
	private static int alpha;
	private static int borderWidth, borderHeight, borderLeft, borderTop;

	public void setOpacity(int alp) {
		alpha = alp;
		repaint();
	}

	public RoundedTextField(int cols) {
		super(cols);
		alpha = 230;

		setOpaque(false);
		setBorder(null);
	}

	protected void paintComponent(Graphics g) {
		int width = getWidth() - getInsets().left - getInsets().right;
		int height = getHeight() - getInsets().top - getInsets().bottom;

		// Paint a rounded rectangle in the background.
		g.setColor(new Color(255, 255, 153, alpha));

		g.fillRoundRect(getInsets().left, getInsets().top, width, height, 50,
				1000);
		borderLeft = getInsets().left;
		borderTop = getInsets().top;
		borderWidth = width;
		borderHeight = height;

		super.paintComponent(g);
	}

	public static class RoundedBorder extends AbstractBorder {

		public Insets getBorderInsets(Component c, Insets insets) {
			insets.left = insets.top = insets.right = insets.bottom = 8;
			return insets;
		}

		public void paintBorder(Component c, Graphics g, int x, int y,
				int width, int height) {
			g.translate(x, y);
			g.setColor(new Color(255, 153, 0, alpha));

			g.drawRoundRect(borderLeft, borderTop, borderWidth, borderHeight,
					50, 1000);
			g.translate(-x, -y);
		}

		public boolean isBorderOpaque() {
			return true;
		}
	}
}
